{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import turtle # import the turtle module\n",
    "\n",
    "# create a window and a turtle object\n",
    "window = turtle.Screen() # create a window\n",
    "car = turtle.Turtle() # create a turtle object\n",
    "\n",
    "# draw the wheels\n",
    "car.color(\"black\") # set the pen color to black\n",
    "car.fillcolor(\"grey\") # set the fill color to grey\n",
    "car.penup() # lift the pen up so it doesn't draw a line\n",
    "car.goto(-50,-50) # move the turtle to the specified position\n",
    "car.pendown() # put the pen down to start drawing\n",
    "car.begin_fill() # begin filling the circle\n",
    "car.circle(20) # draw a circle with radius 20\n",
    "car.end_fill() # end filling\n",
    "\n",
    "car.penup() # lift the pen up so it doesn't draw a line\n",
    "car.goto(50,-50) # move the turtle to the specified position\n",
    "car.pendown() # put the pen down to start drawing\n",
    "car.begin_fill() # begin filling the circle\n",
    "car.circle(20) # draw a circle with radius 20\n",
    "car.end_fill() # end filling\n",
    "\n",
    "# draw the car body\n",
    "car.penup() # lift the pen up so it doesn't draw a line\n",
    "car.goto(-50,-30) # move the turtle to the specified position\n",
    "car.pendown() # put the pen down to start drawing\n",
    "car.fillcolor(\"red\") # set the fill color to red\n",
    "car.begin_fill() # begin filling the rectangle\n",
    "car.forward(100) # move forward 100 pixels and draw a straight line\n",
    "car.left(90) # turn left 90 degrees\n",
    "car.forward(30) # move forward 30 pixels and draw a straight line\n",
    "car.left(90)\n",
    "car.forward(100)\n",
    "car.end_fill() # end filling\n",
    "\n",
    "# keep the window open until the user closes it\n",
    "window.mainloop()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 50 0\n",
      "1 60 50\n",
      "2 70 110\n",
      "3 90 180\n",
      "270 4\n",
      "67.5\n"
     ]
    }
   ],
   "source": [
    "scores = [50,60,70,90]\n",
    "\n",
    "total = 0\n",
    "for i,val in enumerate(scores):\n",
    "    print(i, val, total)\n",
    "    total = total + val\n",
    "\n",
    "print(total, len(scores))\n",
    "arg = total / len(scores)\n",
    "print(arg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9\n",
      "9\n",
      "6\n",
      "2\n",
      "9\n",
      "8\n",
      "10\n",
      "10\n",
      "10\n",
      "6\n"
     ]
    }
   ],
   "source": [
    "from random import random, randint\n",
    "\n",
    "for i in range(10):\n",
    "    print(randint(1,10))\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import turtle # import the turtle module\n",
    "from random import randint\n",
    "\n",
    "# create a window and a turtle object\n",
    "window = turtle.Screen() # create a window\n",
    "\n",
    "\n",
    "class MyCar(object):\n",
    "\n",
    "    def __init__(self, wheel_color, body_color):\n",
    "        self.wheel_color = wheel_color\n",
    "        self.body_color = body_color\n",
    "\n",
    "    def draw(self, x=None, y=None):\n",
    "        if x is None:\n",
    "            x = randint(-400, 400)\n",
    "        if y is None:\n",
    "            y = randint(-400, 400)\n",
    "        car_center_point = (x, y)\n",
    "        car = turtle.Turtle()\n",
    "        # draw the wheels\n",
    "        car.color(\"black\") # set the pen color to black\n",
    "        car.fillcolor(self.wheel_color) # set the fill color to grey\n",
    "        car.penup() # lift the pen up so it doesn't draw a line\n",
    "        car.goto(-50+car_center_point[0],-50+car_center_point[1]) # move the turtle to the specified position\n",
    "        car.pendown() # put the pen down to start drawing\n",
    "        car.begin_fill() # begin filling the circle\n",
    "        car.circle(20) # draw a circle with radius 20\n",
    "        car.end_fill() # end filling\n",
    "\n",
    "        car.penup() # lift the pen up so it doesn't draw a line\n",
    "        car.goto(50+car_center_point[0],-50+car_center_point[1]) # move the turtle to the specified position\n",
    "        car.pendown() # put the pen down to start drawing\n",
    "        car.begin_fill() # begin filling the circle\n",
    "        car.circle(20) # draw a circle with radius 20\n",
    "        car.end_fill() # end filling\n",
    "\n",
    "        # draw the car body\n",
    "        car.penup() # lift the pen up so it doesn't draw a line\n",
    "        car.goto(-50+car_center_point[0],-30+car_center_point[1]) # move the turtle to the specified position\n",
    "        car.pendown() # put the pen down to start drawing\n",
    "        car.fillcolor(self.body_color) # set the fill color to red\n",
    "        car.begin_fill() # begin filling the rectangle\n",
    "        car.forward(100) # move forward 100 pixels and draw a straight line\n",
    "        car.left(90) # turn left 90 degrees\n",
    "        car.forward(30) # move forward 30 pixels and draw a straight line\n",
    "        car.left(90)\n",
    "        car.forward(100)\n",
    "        car.end_fill() # end filling\n",
    "\n",
    "    def change_wheel_color(self, new_color):\n",
    "        self.wheel_color = new_color\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    car1 = MyCar(\"yellow\", \"red\")\n",
    "    car1.draw(50,0)\n",
    "    car1.draw()\n",
    "\n",
    "    # keep the window open until the user closes it\n",
    "    window.mainloop()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Learn Python on Line\n",
    "\n",
    "https://www.freecodecamp.org/learn/scientific-computing-with-python/\n",
    "\n",
    "https://projects.raspberrypi.org/en/pathways/python-intro"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Design a object \n",
    "# Example 1 - student\n",
    "\n",
    "class Student(object):\n",
    "\n",
    "    def __init__(self, name, age, profession):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "        self.profession = profession\n",
    "\n",
    "    def print_info(self):\n",
    "        print(\"%s's age is %s，his/her profession is %s\" % (self.name, self.age, self.profession))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Example 2\n",
    "import turtle\n",
    "\n",
    "class Bomb:\n",
    "    \"\"\"\n",
    "    This is a Python class called \"Bomb\" that defines an object with the properties of x, y, size, and color. \n",
    "    The draw() method is used to draw a shape of a bomb using the turtle module. \n",
    "    It starts by creating a turtle object and setting its speed to 0, then it moves the pen to the coordinates (x,y) \n",
    "    and sets the fill color to the specified color. The for loop will execute 12 times and in each iteration, \n",
    "    it draws a line of length equal to the size and turns left by 30 degrees. \n",
    "    Finally, it ends the fill and hides the turtle, showing the drawing on the screen using the turtle.done() method.\n",
    "    \"\"\"\n",
    "    def __init__(self, x=0, y=0, size=10, color='black'):\n",
    "        self.x = x  # 炸弹图像中心点的 x 坐标\n",
    "        self.y = y  # 炸弹图像中心点的 y 坐标\n",
    "        self.size = size  # 炸弹图像大小\n",
    "        self.color = color  # 炸弹颜色\n",
    "\n",
    "    def draw(self):\n",
    "        t = turtle.Turtle()\n",
    "        t.speed(0)  # 设置画笔速度\n",
    "        t.penup()  # 抬起画笔\n",
    "        t.goto(self.x, self.y)  # 移动画笔到指定位置\n",
    "        t.pendown()  # 放下画笔\n",
    "        t.fillcolor(self.color)  # 设置填充颜色\n",
    "        t.begin_fill()  # 开始填充形状\n",
    "        for _ in range(12):  # 循环绘制 12 条线段\n",
    "            t.forward(self.size)  # 向前移动画笔\n",
    "            t.left(30)  # 左转 30 度\n",
    "        t.end_fill()  # 结束填充形状\n",
    "        t.hideturtle()  # 隐藏画笔\n",
    "        turtle.done()  # 显示画布并等待关闭\n",
    "\n",
    "\n",
    "bomb = Bomb(x=0, y=0, size=50, color='red')\n",
    "bomb.draw()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
